package org.rcloud.io.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.rcloud.io.Utils.PageVo;
import org.rcloud.io.Utils.ResultVo;
import org.rcloud.io.mapper.ArticleTable05MappingMapper;
import org.rcloud.io.mapper.RuralMapper;
import org.rcloud.io.mapper.RuralSpecialtyMappingMapper;
import org.rcloud.io.model.ArticleTable05Mapping;
import org.rcloud.io.model.Rural;
import org.rcloud.io.model.RuralSpecialtyMapping;
import org.rcloud.io.model.Ruralfile;
import org.rcloud.io.service.RuralService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
 * Created by Administrator on 2019/3/12.
 */
@Service(value="RuralService")
public class RuralServiceImpl implements RuralService {

    @Autowired
    private RuralMapper ruralMapper;

    @Autowired
    private ArticleTable05MappingMapper atmm;

    @Autowired
    private RuralSpecialtyMappingMapper rsmm;

    @Override
    public PageVo<Rural> findRural() {

        try {
            List<Rural> list = ruralMapper.selectAll();
            return PageVo.setOk(list);
        } catch (Exception e) {
            e.printStackTrace();
            return PageVo.setError(e.getMessage());
        }

    }

    @Override
    public ResultVo findByEid(String eid) {

        try {
            Rural rural = ruralMapper.selectByEid(eid);
            return ResultVo.setOK(rural);
        } catch (Exception e) {
            e.printStackTrace();
            return ResultVo.setError(e.getMessage());
        }
    }

    @Override
    public ResultVo saveRural(Rural rural) {

        if (rural.equals("") || rural == null) {
            return ResultVo.setError("传入数据为空");
        } else {
            try {
                ruralMapper.insertSelective(rural);
                return ResultVo.setOK(null);
            } catch (Exception e) {
                e.printStackTrace();
                return ResultVo.setError(e.getMessage());
            }
        }

    }

    @Override
    public ResultVo findById(int id) {

        try {
            Rural rural = ruralMapper.selectByPrimaryKey(id);
            return ResultVo.setOK(rural);
        } catch (Exception e) {
            e.printStackTrace();
            return ResultVo.setError(e.getMessage());
        }

    }

    @Override
    public ResultVo updateRural(Rural rural) {

        try {
            ruralMapper.updateByPrimaryKeySelective(rural);
            return ResultVo.setOK(rural);
        } catch (Exception e) {
            e.printStackTrace();
            return ResultVo.setError(e.getMessage());
        }

    }

    @Override
    public ResultVo updateArtid(String nameTitle, String INF01) {

        try {
            int i = ruralMapper.updateByTitle(nameTitle, INF01);
            return ResultVo.setOK(i);
        } catch (Exception e) {
            e.printStackTrace();
            return ResultVo.setError(e.getMessage());
        }

    }

    @Override
    public ResultVo saveSite(ArticleTable05Mapping articleTable05Mapping) {
        if (articleTable05Mapping.equals("") || articleTable05Mapping == null) {
            return ResultVo.setError("传入数据为空");
        } else {
            Integer aId = articleTable05Mapping.getaId();
            ArticleTable05Mapping byAId = null;
            try {
                byAId = atmm.findByAId(aId);

            } catch (Exception e) {
                e.printStackTrace();
            }
            try {
                if (byAId != null) {
                    articleTable05Mapping.setId(byAId.getId());
                    atmm.updateByPrimaryKey(articleTable05Mapping);
                    return ResultVo.setOK("修改成功");
                } else {
                    atmm.insert(articleTable05Mapping);
                    return ResultVo.setOK("添加成功");
                }
            } catch (Exception e) {
                e.printStackTrace();
                return ResultVo.setError(e.getMessage());
            }
        }
    }

    @Override
    public ResultVo saveMapping(RuralSpecialtyMapping ruralSpecialtyMapping) {
        if (ruralSpecialtyMapping.equals("") || ruralSpecialtyMapping == null) {
            return ResultVo.setError("传入数据为空");
        } else {
            int i = rsmm.insert(ruralSpecialtyMapping);
            if (i > 0) {
                return ResultVo.setOK(i);
            } else {
                return ResultVo.setError("添加失败");
            }
        }
    }

    @Override
    public PageInfo<Rural> selectByKey(int pageNum, int pageSize, Map map) {
        PageHelper.startPage(pageNum,pageSize,true);
        List<Rural> ruralList = ruralMapper.selectByKey(map);
        PageInfo<Rural> ruralPageInfo = new PageInfo<>(ruralList);
        return ruralPageInfo;
    }
}
